<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html" ; charset="utf-8" />
    <title>业务小组管理</title>
    <link href="../../../css/main.css" rel="stylesheet" type="text/css">
    <link href="../../../bstable/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="../../../bstable/css/bootstrap-table.css" rel="stylesheet" type="text/css">

    <style>
        .bootstrap-table {
            padding: 20px;
        }

        .dialog-form {
            width: 350px;
        }

        .dialog-form .form_input {
            width: 70%;
        }

        .dialog-form .form-control {
            width: 70%;
            display: inline-block;
        }

        .dialog-form .p_line {
            border: none;
        }
    </style>
</head>

<body>
    <div class="tabe_div">
        <p class="p_line text_ient" style="margin-bottom: -10px">
            <a class="find_a">业务小组：</a>
            <select class="form_select" style="padding-left: 1%;" id="group">
            </select>
            <a class="find_a">主管姓名：</a>
            <input class="form_input text_ient" type="text" maxlength="10" placeholder="请输入姓名" id="supervisor" />
            <a class="find_a">主管手机号：</a>
            <input class="form_input text_ient" type="text" maxlength="11" placeholder="请输入手机号" id="supervisor_tel" />
            <input type="button" value="查询" class="but_find" id="search" />
            <input type="button" value="创建" onclick="create()" class="but_find" id="create" />
        </p>
        <table data-url="json/data_alae_list.json" id="table" class="table_style" style="margin: 0 auto">
        </table>
    </div>
    <script src="../../../js/jquery.js"></script>
    <script src="../../../js/common.js"></script>
    <script src="../../../js/date/js/laydate.js"></script>
    <script src="../../../bstable/js/bootstrap.min.js"></script>
    <script src="../../../bstable/js/bootstrap-table.js"></script>
    <script src="../../../bstable/js/bootstrap-table-zh-CN.min.js"></script>
    <script src="../../../layer_v2.1/layer/layer.js"></script>
    <script type="text/javascript">
        localStorage.setItem('name', '用户管理/业务组管理')
        queryGroup();
        function changeFee(ids) {
            var arr = ids.split(',')
            $.ajax({
                url: baseUrl + "studentInfo/setStudentFeeFlag",
                method: "get",
                headers: {
                    "Authorization": JSON.parse(localStorage.getItem('userInfo')).token
                },
                data: {
                    studentId: arr[0],
                    feeId: arr[1],
                    feeFlag: arr[2]
                },
                success: function (data) {
                    $('#table').bootstrapTable('refresh')
                    alert('操作成功')
                },
                error() {
                    alert('操作失败')
                }
            })
        }

        //查询业务小组
        function queryGroup(obj) {
            $.ajax({
                url: baseUrl + "businessdirector/listAll",
                method: "Post",
                headers: {
                    "Authorization": JSON.parse(localStorage.getItem('userInfo')).token
                },
                data: "",
                success: function (data) {
                    var html_ = "<option value=\"\">请选择</option>";
                    for (var i = 0; i < data.data.length; i++) {
                        var item = data.data[i];
                        html_ += "<option  value=\"" + item.id + "\" >" + item.businessTeam +
                            "</option>";
                    }
                    $("#group").html(html_);
                }
            })
        }

        var getSupervisorNameByTel = function (obj) {
            console.log($(obj).val())
            var val = $(obj).val();
            if (val.length == 11) {
                if (!(/^1[3456789]\d{9}$/.test(val))) {
                    alert("手机号码有误");
                    return false;
                }
                $.ajax({
                    url: baseUrl + "user/getUserInfoByPhone?phone=" + obj.value,
                    method: "GET",
                    headers: {
                        "Authorization": JSON.parse(localStorage.getItem('userInfo')).token
                    },
                    success: function (data) {
                        if (!data.data.name) {
                            $("#zg_name").val('不存在该主管！')
                            return false;
                        }
                        var name = data.data.name;
                        window.teacherId = data.data.id;
                        $("#zg_name").val(name);

                    }
                })
            }
        }

        $(function () {
            $('#search').on('click', function () {
                $('#table').bootstrapTable('refresh')
            })
            var perm = JSON.parse(localStorage.getItem('userInfo')).perm
            $('#table')
                .bootstrapTable({
                    method: "get",
                    striped: true,
                    singleSelect: false,
                    url: baseUrl + 'businessdirector/list',
                    dataType: "json",
                    sidePagination: "server",
                    pagination: true, //分页
                    pageSize: 10,
                    pageNumber: 1,
                    // rowStyle: rowStyle,
                    search: false, //显示搜索框
                    contentType: "application/x-www-form-urlencoded",
                    ajaxOptions: {
                        headers: { "Authorization": JSON.parse(localStorage.getItem('userInfo')).token }
                    },
                    queryParams: function (params) {
                        params.businessTeam = $('#group').val() ? $('#group').val() : ''
                        params.supervisorsName = $('#supervisor').val().trim()
                        params.supervisorsPhone = $('#supervisor_tel').val().trim()
                        params.offset = params.offset / params.limit
                        params.page = parseInt(params.offset) + 1
                        delete params.order
                        console.log(params)
                        return params
                    },
                    responseHandler: function (res) {
                        console.log(res)
                        var list = res.data.list;
                        return {
                            rows: list,
                            total: res.data.totalCount
                        }
                    },
                    columns: [{
                        field: 'no',
                        title: '序号',
                        sortable: true,
                        align: "center",
                        formatter: function (value, row, index) {
                            var pageSize = $('#table').bootstrapTable('getOptions').pageSize;
                            var pageNumber = $('#table').bootstrapTable('getOptions').pageNumber;
                            return pageSize * (pageNumber - 1) + index + 1;
                        }
                    }, {
                        title: '业务小组',
                        field: 'businessTeam',
                        align: 'center',
                        valign: 'middle'
                    }, {
                        title: '主管姓名',
                        field: 'supervisorsName',
                        align: 'center'
                    }, {
                        title: '主管手机号',
                        field: 'supervisorsPhone',
                        align: 'center'
                    }, {
                        title: '创建日期',
                        field: 'createDate',
                        align: 'center'
                    }, {
                        title: '操作',
                        field: 'id',
                        align: 'center',
                        width: '160',
                        formatter: function (value, row) {
                            var e = '<a href="#" onclick="config(\'' + row.id + '\', \'' + row.businessTeam + '\', \'' + row.supervisorsName + '\', \'' + row.supervisorsPhone + '\',\'' + row.createDate + '\')">配置</a> ';
                            return e;
                        }
                    }]
                });
        });

        

        //配置
        function config(id,businessTeam,supervisorsName,supervisorsPhone,createDate) {
            console.log(id)
            console.log(businessTeam)
            console.log(supervisorsName)
            console.log(supervisorsPhone)
            window.phoneNum = supervisorsPhone
            // window.location.href = './money.html?type=look&from=jiaofei&id=' + id + '&idNo=' + idNo + '&flag=' + flag
            $('#group_name').val(businessTeam);
            $('#zg_tel').val(supervisorsPhone);
            layerOpen(2,id,businessTeam,supervisorsName,supervisorsPhone,createDate);
        }

        //创建
        function create() {
            layerOpen(1);
        }

        Date.prototype.Format = function (fmt) { // author: meizz
            var o = {
                "M+": this.getMonth() + 1, // 月份
                "d+": this.getDate(), // 日
                "h+": this.getHours(), // 小时
                "m+": this.getMinutes(), // 分
                "s+": this.getSeconds(), // 秒
                "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
                "S": this.getMilliseconds() // 毫秒
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }
        var getTime = function getTime() {
            return new Date().Format("yyyy-MM-dd");
        }

        /**
            * 新增业务小组
            */
        var saveGroup = function saveGroup(type,id) {

            console.log("新增")
            var formData = new FormData();
            var name = $('#group_name').val();
            var zg_tel = $('#zg_tel').val();
            var zg_name = $('#zg_name').val();
            if (!name) {
                alert('请输入小组名称')
                return false;
            }
            if (!zg_tel) {
                alert('请输入主管电话')
                return false;
            }
            if (!(/^1[3456789]\d{9}$/.test(zg_tel))) {
                alert("手机号码有误");
                return false;
            }
            console.log("name:" + name + "  zg_tel:" + zg_tel)
            if (zg_name == "不存在该主管！") {
                alert("该主管不存在，请确认电话号码")
                return false
            }
            if(id){
                formData.append('id', id);
            }

            formData.append('businessTeam', name);
            formData.append('teacherId', window.teacherId);
            formData.append('supervisorsName', zg_name);
            formData.append('supervisorsPhone', zg_tel);
            var date = new Date().Format("yyyy-MM-dd");
            formData.append('date', date);
            $.ajax({
                url: baseUrl + "businessdirector/saveOrUpdate",
                method: "post",
                headers: {
                    "Authorization": JSON.parse(localStorage.getItem('userInfo')).token
                },
                data: formData,
                processData: false,
                contentType: false,
                mimeType: "multipart/form-data",
                success: function (data) {
                    data = JSON.parse(data);
                    if (data.code == 500) {
                        alert(data.message)
                        // layer.msg(data.message);
                        layer.closeAll();
                    } else {
                        if (window.teacherId) {
                           addTeacher(type, data.data);
                        } else {
                            layer.closeAll();
                            $('#table').bootstrapTable("refresh");
                        }
                    }
                    // $('#table').bootstrapTable("refresh");
                }
            })


        }

        function addTeacher(type, id) {
            if (type === 1) {
                window.phoneNum = ''
            }
                var zg_tel = $('#zg_tel').val();
                var zg_name = $('#zg_name').val();
                var formData = new FormData();

                formData.append('businessId', id);
                formData.append('teacherId', window.teacherId);
                formData.append('teacherName', zg_name);
                formData.append('mobile', window.phoneNum);
                $.ajax({
                url: baseUrl + "businessdirectorteacher/updateOrSave",
                method: "post",
                headers: {
                    "Authorization": JSON.parse(localStorage.getItem('userInfo')).token
                },
                data: formData,
                processData: false,
                contentType: false,
                mimeType: "multipart/form-data",
                success: function (data) {
                    data = JSON.parse(data);
                    layer.closeAll();
                    if (data.code == 500) {
                        alert(data.message)
                        // layer.msg(data.message);
                    } else {
                        layer.msg('保存成功');
                    }
                    $('#table').bootstrapTable("refresh");
                }
            })
        }

        //type 1新增  2修改
        function layerOpen(type,id,businessTeam,supervisorsName,supervisorsPhone,createDate) {
            var html =`<div class="dialog-form"><div class="p_line text_ient lx-row">
                            <p class="col lx-col-50">
                    <a class="find_a">创建时间：</a>
                    ${getTime()}
                </p>
                            <p class="col lx-col-50">
                    <a class="find_a">业务小组：&nbsp;</a>
                    <input id="group_name" type="text" maxlength="11" style="width: 66%;" class="form_input text_ient" placeholder="请输入名称"/>
                </p>
                <p class="col lx-col-50">
                    <a class="find_a">
                        主管手机号：</a>
                    <input id="zg_tel" type="text" maxlength="11" style="width: 66%;" class="form_input text_ient" placeholder="请输入11位手机号" oninput="getSupervisorNameByTel(this)"/>
                </p>
                <p class="col lx-col-50">
                    <a class="find_a">
                        主管姓名：&nbsp;</a>
                    <input id="zg_name" type="text" maxlength="10" class="form_input text_ient" placeholder="主管姓名" disabled/>
                </p>
            </div></div>`;
            if(type == 2){
                html =`<div class="dialog-form"><div class="p_line text_ient lx-row">
                            <p class="col lx-col-50">
                    <a class="find_a">创建时间：</a>
                    ${createDate}
                </p>
                            <p class="col lx-col-50">
                    <a class="find_a">业务小组：&nbsp;</a>
                    <input id="group_name" type="text" maxlength="11" style="width: 66%;" class="form_input text_ient" placeholder="请输入名称" value="${businessTeam}"/>
                </p>
                <p class="col lx-col-50">
                    <a class="find_a">
                        主管手机号：</a>
                    <input id="zg_tel" type="text" maxlength="11" style="width: 66%;" class="form_input text_ient" placeholder="请输入11位手机号" oninput="getSupervisorNameByTel(this)" value="${supervisorsPhone}"/>
                </p>
                <p class="col lx-col-50">
                    <a class="find_a">
                        主管姓名：&nbsp;</a>
                    <input id="zg_name" type="text" maxlength="10" class="form_input text_ient" placeholder="主管姓名" disabled value="${supervisorsName}"/>
                </p>
            </div></div>`;
            }
            layer.open({
                title: '创建业务小组',
                type: 1,
                // closeBtn: 0, //不显示关闭按钮
                anim: 2,
                yes: function (index, layero) {
                    saveGroup(type,id);

                    //do something
                    // console.log(index, layero);
                    // layer.close(index); //如果设定了yes回调，需进行手工关闭
                },
                btn: ['保存'],
                // shadeClose: true, //开启遮罩关闭
                content: html
            });
        }

        function rowStyle(item, index) {
            style = item.colorFlag == 1 ? { css: { 'color': '#ed5565' } } : {}
            return style;
        }
    </script>
</body>

</html>